/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.BeanUtils
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Service
 *  org.springframework.transaction.annotation.Transactional
 *  tk.mybatis.mapper.entity.Example
 */
package com.gys.spliteService.impl;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.exception.BusinessException;
import com.gys.mapper.GaiaProductBusinessMapper;
import com.gys.mapper.GaiaSdSaleDMapper;
import com.gys.mapper.GaiaSdSaleHMapper;
import com.gys.mapper.GaiaSdSaleRecipelMapper;
import com.gys.mapper.GaiaSdStoreDataMapper;
import com.gys.mapper.GaiaUserDataMapper;
import com.gys.mapper.entity.GaiaProductBusiness;
import com.gys.mapper.entity.GaiaRetailPrice;
import com.gys.mapper.entity.GaiaSdSaleD;
import com.gys.mapper.entity.GaiaSdSaleH;
import com.gys.mapper.entity.GaiaSdSaleRecipel;
import com.gys.mapper.entity.GaiaSdStoreData;
import com.gys.mapper.entity.GaiaUserData;
import com.gys.spliteService.SalesReceiptsService;
import com.gys.spliteService.data.GetCreateSaleOutData;
import com.gys.spliteService.data.GetPdAndExpOutData;
import com.gys.spliteService.data.GetQueryProductInData;
import com.gys.spliteService.data.GetQueryProductOutData;
import com.gys.spliteService.data.GetRestOrderDetailOutData;
import com.gys.spliteService.data.GetRestOrderInfoOutData;
import com.gys.spliteService.data.GetSalesReceiptsTableOutData;
import com.gys.spliteService.data.RecipelInfoInputData;
import com.gys.spliteService.data.SelectRecipelDrugsOutData;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

@Service
public class SalesReceiptsImpl
implements SalesReceiptsService {
    private static final Logger log = LoggerFactory.getLogger(SalesReceiptsImpl.class);
    @Autowired
    private GaiaSdSaleHMapper gaiaSdSaleHMapper;
    @Autowired
    private GaiaSdSaleDMapper gaiaSdSaleDMapper;
    @Autowired
    private GaiaProductBusinessMapper gaiaProductBusinessMapper;
    @Autowired
    private GaiaSdSaleRecipelMapper gaiaSdSaleRecipelMapper;
    @Autowired
    private GaiaUserDataMapper gaiaUserDataMapper;
    @Autowired
    private GaiaSdStoreDataMapper sdStoreDataMapper;

    @Override
    @Transactional
    public void createSale(GetCreateSaleOutData inData) {
        GaiaSdSaleH gaiaSdSaleH = inData.getGaiaSdSaleH();
        GaiaSdSaleH gaiaSdSaleHSource = (GaiaSdSaleH)this.gaiaSdSaleHMapper.selectByPrimaryKey(gaiaSdSaleH);
        if (ObjectUtil.isEmpty(gaiaSdSaleHSource)) {
            gaiaSdSaleH.setGsshUpload("0");
            gaiaSdSaleH.setGsshHykPhone(inData.getGsshHykPhone());
            this.gaiaSdSaleHMapper.insert(gaiaSdSaleH);
        } else {
            BeanUtils.copyProperties(gaiaSdSaleH, gaiaSdSaleHSource);
            gaiaSdSaleHSource.setGsshUpload("0");
            gaiaSdSaleHSource.setGsshHykPhone(inData.getGsshHykPhone());
            this.gaiaSdSaleHMapper.updateByPrimaryKey(gaiaSdSaleHSource);
        }
        for (GaiaSdSaleD item : inData.getGaiaSdSaleDList()) {
            this.gaiaSdSaleDMapper.deleteByPrimaryKey(item);
            this.gaiaSdSaleDMapper.insert(item);
        }
    }

    @Override
    public List<GetRestOrderInfoOutData> getRestOrder(GaiaSdSaleH inData) {
        Example example = new Example(GaiaSdSaleH.class);
        example.createCriteria().andEqualTo("clientId", inData.getClientId()).andEqualTo("gsshBrId", inData.getGsshBrId()).andEqualTo("gsshDate", inData.getGsshDate());
        example.setOrderByClause("GSSH_TIME DESC");
        List<GaiaSdSaleH> gaiaSdSaleHList = this.gaiaSdSaleHMapper.selectByExample(example);
        ArrayList<GetRestOrderInfoOutData> restOrderInfoOutDataList = new ArrayList<GetRestOrderInfoOutData>();
        for (GaiaSdSaleH item : gaiaSdSaleHList) {
            GetRestOrderInfoOutData outData = new GetRestOrderInfoOutData();
            outData.setGsshBillNo(item.getGsshBillNo());
            outData.setGsshDate(item.getGsshDate());
            outData.setGsshHykNo(item.getGsshHykNo());
            outData.setGsshTime(item.getGsshTime());
            outData.setGsshYsAmt(String.format("%.2f", item.getGsshYsAmt()));
            restOrderInfoOutDataList.add(outData);
        }
        return restOrderInfoOutDataList;
    }

    @Override
    public List<GetRestOrderDetailOutData> getRestOrderDetail(GaiaSdSaleD inData) {
        List gaiaSdSaleDList = this.gaiaSdSaleDMapper.select(inData);
        ArrayList<GetRestOrderDetailOutData> outData = new ArrayList<GetRestOrderDetailOutData>();
        for (int k = 0; k < gaiaSdSaleDList.size(); ++k) {
            GaiaUserData gaiaUserData;
            GaiaSdSaleD item = (GaiaSdSaleD)gaiaSdSaleDList.get(k);
            GetRestOrderDetailOutData orderDetailOutData = new GetRestOrderDetailOutData();
            BeanUtils.copyProperties(item, orderDetailOutData);
            GaiaProductBusiness gaiaProductBusiness = new GaiaProductBusiness();
            gaiaProductBusiness.setClient(inData.getClientId());
            gaiaProductBusiness.setProSite(inData.getGssdBrId());
            gaiaProductBusiness.setProSelfCode(orderDetailOutData.getGssdProId());
            gaiaProductBusiness.setProStatus("0");
            gaiaProductBusiness = (GaiaProductBusiness)this.gaiaProductBusinessMapper.selectOne(gaiaProductBusiness);
            orderDetailOutData.setGssdProName(gaiaProductBusiness.getProName());
            orderDetailOutData.setSpec(gaiaProductBusiness.getProSpecs());
            orderDetailOutData.setProFactoryName(gaiaProductBusiness.getProFactoryName());
            orderDetailOutData.setProUsage(gaiaProductBusiness.getProUsage());
            orderDetailOutData.setProContraindication(gaiaProductBusiness.getProContraindication());
            GetQueryProductInData queryProductInData = new GetQueryProductInData();
            queryProductInData.setBrId(inData.getGssdBrId());
            queryProductInData.setClientId(inData.getClientId());
            queryProductInData.setNameOrCode(orderDetailOutData.getGssdProId());
            GetSalesReceiptsTableOutData getSalesReceiptsTableOutData = this.queryProductDetail(queryProductInData);
            getSalesReceiptsTableOutData.setNum(item.getGssdQty());
            getSalesReceiptsTableOutData.setIndex(String.valueOf(k + 1));
            getSalesReceiptsTableOutData.setPrcAmount(String.format("%.2f", item.getGssdPrc1()));
            getSalesReceiptsTableOutData.setPrice(String.format("%.2f", item.getGssdPrc2()));
            getSalesReceiptsTableOutData.setTotal(String.format("%.2f", item.getGssdAmt()));
            getSalesReceiptsTableOutData.setStatus(item.getGssdProStatus());
            float v = Float.valueOf(String.format("%.2f", item.getGssdPrc2().divide(item.getGssdPrc1()).multiply(new BigDecimal(100)))).floatValue();
            getSalesReceiptsTableOutData.setDisc(String.valueOf((int)v));
            if (ObjectUtil.isNotEmpty(orderDetailOutData.getGssdSalerId())) {
                getSalesReceiptsTableOutData.setEmpId(orderDetailOutData.getGssdSalerId());
                gaiaUserData = new GaiaUserData();
                gaiaUserData.setClient(inData.getClientId());
                gaiaUserData.setUserId(orderDetailOutData.getGssdSalerId());
                gaiaUserData = (GaiaUserData)this.gaiaUserDataMapper.selectOne(gaiaUserData);
                getSalesReceiptsTableOutData.setEmpName(gaiaUserData.getUserNam());
            }
            if (ObjectUtil.isNotEmpty(orderDetailOutData.getGssdDoctorId())) {
                getSalesReceiptsTableOutData.setDoctorId(orderDetailOutData.getGssdDoctorId());
                gaiaUserData = new GaiaUserData();
                gaiaUserData.setClient(inData.getClientId());
                gaiaUserData.setUserId(orderDetailOutData.getGssdDoctorId());
                gaiaUserData = (GaiaUserData)this.gaiaUserDataMapper.selectOne(gaiaUserData);
                getSalesReceiptsTableOutData.setDoctorName(gaiaUserData.getUserNam());
            }
            GetQueryProductInData pdInData = new GetQueryProductInData();
            pdInData.setClientId(inData.getClientId());
            pdInData.setBrId(inData.getGssdBrId());
            pdInData.setNameOrCode(orderDetailOutData.getGssdProId());
            pdInData.setGssbBatchNo(orderDetailOutData.getGssdBatchNo());
            GetPdAndExpOutData pdAndExpOutData = this.gaiaProductBusinessMapper.queryBatchNoAndExp(pdInData).get(0);
            getSalesReceiptsTableOutData.setPdAndExpData(pdAndExpOutData);
            GetQueryProductInData beInData = new GetQueryProductInData();
            beInData.setClientId(inData.getClientId());
            beInData.setBrId(inData.getGssdBrId());
            pdInData.setNameOrCode(orderDetailOutData.getGssdProId());
            List<GetPdAndExpOutData> pdAndExpOutDataList = this.queryBatchNoAndExp(beInData);
            getSalesReceiptsTableOutData.setPdAndExpOutDataList(pdAndExpOutDataList);
            orderDetailOutData.setGetSalesReceiptsTableOutData(getSalesReceiptsTableOutData);
            outData.add(orderDetailOutData);
        }
        GaiaSdSaleH gaiaSdSaleH = new GaiaSdSaleH();
        gaiaSdSaleH.setClientId(inData.getClientId());
        gaiaSdSaleH.setGsshBillNo(inData.getGssdBillNo());
        gaiaSdSaleH = (GaiaSdSaleH)this.gaiaSdSaleHMapper.selectByPrimaryKey(gaiaSdSaleH);
        int count = Integer.valueOf(gaiaSdSaleH.getGsshCallQty()) + 1;
        gaiaSdSaleH.setGsshCallQty(String.valueOf(count));
        this.gaiaSdSaleHMapper.updateByPrimaryKey(gaiaSdSaleH);
        return outData;
    }

    @Override
    public GetSalesReceiptsTableOutData queryProductDetail(GetQueryProductInData inData) {
        GetSalesReceiptsTableOutData out = this.gaiaProductBusinessMapper.queryProductDetail(inData);
        if (ObjectUtil.isNotEmpty(out)) {
            DecimalFormat decimalFormat = new DecimalFormat("##########.##########");
            out.setPrcAmount(decimalFormat.format(new BigDecimal(out.getPrcAmount())));
            out.setPrice(decimalFormat.format(new BigDecimal(out.getPrice())));
            out.setTotal(decimalFormat.format(new BigDecimal(out.getTotal())));
        }
        return out;
    }

    @Override
    public List<GetPdAndExpOutData> queryBatchNoAndExp(GetQueryProductInData inData) {
        List<GetPdAndExpOutData> outData = this.gaiaProductBusinessMapper.queryBatchNoAndExp(inData);
        return outData;
    }

    @Override
    @Transactional
    public void saveRecipelInfo(RecipelInfoInputData recipelInfoInputData) {
        GaiaSdSaleRecipel gaiaSdSaleRecipelTemp = new GaiaSdSaleRecipel();
        gaiaSdSaleRecipelTemp.setClientId(recipelInfoInputData.getClientId());
        gaiaSdSaleRecipelTemp.setGssrSaleBillNo(recipelInfoInputData.getGssrSaleBillNo());
        this.gaiaSdSaleRecipelMapper.delete(gaiaSdSaleRecipelTemp);
        for (int i = 0; i < recipelInfoInputData.getRecipelDrugsList().size(); ++i) {
            SelectRecipelDrugsOutData selectRecipelDrugsOutData = recipelInfoInputData.getRecipelDrugsList().get(i);
            GaiaSdSaleRecipel gaiaSdSaleRecipel = new GaiaSdSaleRecipel();
            gaiaSdSaleRecipel.setClientId(recipelInfoInputData.getClientId());
            String nextVoucherId = this.gaiaSdSaleRecipelMapper.selectNextVoucherId();
            gaiaSdSaleRecipel.setGssrVoucherId(nextVoucherId);
            if (ObjectUtil.isEmpty(recipelInfoInputData.getGssrType())) {
                gaiaSdSaleRecipel.setGssrType("1");
            }
            gaiaSdSaleRecipel.setGssrBrId(recipelInfoInputData.getGssrBrId());
            gaiaSdSaleRecipel.setGssrBrName(recipelInfoInputData.getGssrBrName());
            gaiaSdSaleRecipel.setGssrDate(recipelInfoInputData.getGssrDate());
            gaiaSdSaleRecipel.setGssrTime(recipelInfoInputData.getGssrTime());
            gaiaSdSaleRecipel.setGssrEmp(recipelInfoInputData.getGssrEmp());
            gaiaSdSaleRecipel.setGssrSaleBrId(recipelInfoInputData.getGssrSaleBrId());
            gaiaSdSaleRecipel.setGssrSaleDate(recipelInfoInputData.getGssrSaleDate());
            gaiaSdSaleRecipel.setGssrSaleBillNo(recipelInfoInputData.getGssrSaleBillNo());
            gaiaSdSaleRecipel.setGssrCustName(recipelInfoInputData.getName());
            gaiaSdSaleRecipel.setGssrCustSex(recipelInfoInputData.getSex());
            gaiaSdSaleRecipel.setGssrCustAge(recipelInfoInputData.getAge());
            gaiaSdSaleRecipel.setGssrCustIdcard(recipelInfoInputData.getCardId());
            gaiaSdSaleRecipel.setGssrCustMobile(recipelInfoInputData.getPhone());
            gaiaSdSaleRecipel.setGssrCheckStatus(recipelInfoInputData.getGssrCheckStatus());
            gaiaSdSaleRecipel.setGssrProId(selectRecipelDrugsOutData.getProCode());
            gaiaSdSaleRecipel.setGssrBatchNo(selectRecipelDrugsOutData.getGssdBatch());
            gaiaSdSaleRecipel.setGssrQty(selectRecipelDrugsOutData.getNum());
            gaiaSdSaleRecipel.setGssrRecipelId(recipelInfoInputData.getCaseId());
            gaiaSdSaleRecipel.setGssrRecipelHospital(recipelInfoInputData.getHospitalName());
            gaiaSdSaleRecipel.setGssrRecipelDepartment(recipelInfoInputData.getOffice());
            gaiaSdSaleRecipel.setGssrRecipelDoctor(recipelInfoInputData.getDoctor());
            gaiaSdSaleRecipel.setGssrSymptom(recipelInfoInputData.getSymptom());
            gaiaSdSaleRecipel.setGssrDiagnose(recipelInfoInputData.getDiacrisis());
            gaiaSdSaleRecipel.setGssrType("1");
            this.gaiaSdSaleRecipelMapper.insert(gaiaSdSaleRecipel);
        }
    }

    @Override
    public String getStoreDyqMaxAmount(GaiaRetailPrice inData) {
        Example example = new Example(GaiaSdStoreData.class);
        example.createCriteria().andEqualTo("clientId", inData.getClent()).andEqualTo("gsstBrId", inData.getPrcStore());
        GaiaSdStoreData storeData = (GaiaSdStoreData)this.sdStoreDataMapper.selectOneByExample(example);
        if (ObjectUtil.isEmpty(storeData)) {
            throw new BusinessException("\u672a\u83b7\u53d6\u5230\u5bf9\u5e94\u95e8\u5e97\u6700\u5927\u62b5\u7528\u91d1\u989d\uff01");
        }
        return storeData.getGsstDyqMaxAmt().toString();
    }

    @Override
    public List<GetQueryProductOutData> queryProduct(GetQueryProductInData inData) {
        List<GetQueryProductOutData> out = this.gaiaProductBusinessMapper.queryProduct(inData);
        for (int i = 0; i < out.size(); ++i) {
            GetQueryProductOutData item = out.get(i);
            item.setIndex(String.valueOf(i + 1));
            DecimalFormat decimalFormat = new DecimalFormat("##########.##########");
            item.setPrcAmount(decimalFormat.format(new BigDecimal(item.getPrcAmount())));
        }
        return out;
    }
}

